package com.catena.web.controller;

import com.catena.core.CatenaControllerBase;
import com.catena.mock.MockRuntimeException;
import com.catena.mock.core.HttpRequestMethod;
import com.catena.mock.core.ScanProjectContext;
import com.catena.mock.param.MockBaseParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hx-pc on 16-5-18.
 */
@RestController
@RequestMapping ("/api")
public class MockDataManageController extends CatenaControllerBase {

    @Autowired
    private ScanProjectContext scanProjectContext;

    @RequestMapping (value = "/mock", method = RequestMethod.POST)
    public void addMock(MockBaseParam mockBaseParam, HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        if (!mockBaseParam.getApiValue().substring(0, 1).equals("/")) {
            throw new MockRuntimeException(401, "请填写完整的url路径");
        }
        mockBaseParam.buildServletRequest(servletRequest);
        getOperation("addMock").start(mockBaseParam);
        returnSuccess(servletRequest, servletResponse);
    }

    @RequestMapping (value = "/mock", method = RequestMethod.GET)
    public void getMock(@RequestParam (required = true) HttpRequestMethod httpRequestMethod, HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException {
        scanProjectContext.getScanContext(servletRequest.getParameter("project")).checkScanMockData();
        if (servletRequest.getParameter("project") == null) {
            throw new MockRuntimeException(404,"not found project");
        }
        if (httpRequestMethod == null) {
            servletRequest.setAttribute("httpRequestMethod", "get");
        } else {
            servletRequest.setAttribute("httpRequestMethod", httpRequestMethod);
        }
        getOperation("getMock").startReturnDataWithObject(servletRequest, servletResponse);
    }

    @RequestMapping (value = "/project", method = RequestMethod.PUT)
    public void addProject(@RequestBody (required = true) String project, HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("project", project.substring(project.indexOf("=") + 1));
        getOperation("addProject").start(map);
        returnSuccess(servletRequest, servletResponse);
    }

    @RequestMapping (value = "/project", method = RequestMethod.DELETE)
    public void deleteProject(@RequestBody (required = true) String project, HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("project", project.substring(project.indexOf("=") + 1));
        getOperation("deleteProject").start(map);
        returnSuccess(servletRequest, servletResponse);
    }

    protected void returnSuccess(HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        servletRequest.setAttribute("data","操作成功");
        getOperation("returnData").startReturnDataWithObject(servletRequest, servletResponse);
    }
}
